package com.yanze.schoolinformation.adapter.rv;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.base.baseadapter.BaseRecyclerViewAdapter;
import com.yanze.schoolinformation.base.baseadapter.BaseRecyclerViewHolder;
import com.yanze.schoolinformation.databinding.ItemSecondbookBinding;
import com.yanze.schoolinformation.model.bean.SecondBookInfoBean;
import com.yanze.schoolinformation.utils.PerfectClickListener;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.view.viewbigimage.ViewBigImageActivity;

/**
 * Created by MrZheng on 2018/5/10.
 */

public class BookAdapter extends BaseRecyclerViewAdapter<SecondBookInfoBean> {

    private Context mContext;

    public BookAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(parent, R.layout.item_secondbook);
    }

    class ViewHolder extends BaseRecyclerViewHolder<SecondBookInfoBean,ItemSecondbookBinding>{

        public ViewHolder(ViewGroup viewGroup, int layoutId) {
            super(viewGroup, layoutId);
        }

        @Override
        public void onBindViewHolder(final SecondBookInfoBean book, int position) {
            binding.setBook(book);
            binding.executePendingBindings();
            binding.ivHeader.setOnClickListener(new PerfectClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    ViewBigImageActivity.start(mContext, C.url.API_IMAGE + book.getHeader(),book.getHeader());

                }
            });

            //设置网上获取的图片集合
            if (book.getImages() != null && !book.getImages().isEmpty()) {
                int spanCount = 3;
                if (book.getImages().size() % 2 == 0) {
                    spanCount = 2;
                }
                if (book.getImages().size() % 3 == 0) {
                    spanCount = 3;
                }
                //使用adapter显示
                binding.rvImage.setLayoutManager(new GridLayoutManager(mContext, spanCount));
                DynamicAdapter.ImageAdapter imageAdapter = new DynamicAdapter.ImageAdapter(mContext);
                imageAdapter.addAll(book.getImages());
                binding.rvImage.setAdapter(imageAdapter);
                // 需加，不然滑动不流畅
                binding.rvImage.setNestedScrollingEnabled(false);
                binding.rvImage.setHasFixedSize(false);
                binding.rvImage.setItemAnimator(new DefaultItemAnimator());
            }
        }
    }
}
